//
//  ILGViewController.m
//  LGUIDemo
//
//  Created by zhangtongle-Pro on 2017/9/4.
//  Copyright © 2017年 Happy. All rights reserved.
//

#import "ILGViewController.h"

@interface ILGViewController () <UITextFieldDelegate>

@end

@implementation ILGViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 解决自定义导航控制器左上角返回按钮后，右侧滑手势失效问题
//    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setScrollViewForHandleKeyboard:(UIScrollView *)scrollViewForHandleKeyboard {
    _scrollViewForHandleKeyboard = scrollViewForHandleKeyboard;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    UITapGestureRecognizer *tapGesForHideKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidClicked)];
    [self.view addGestureRecognizer:tapGesForHideKeyboard];
}

#pragma mark - Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.scrollViewForHandleKeyboard) {
        UIView *nextTF = [self.scrollViewForHandleKeyboard viewWithTag:textField.tag + 1];
        if (nextTF) {
            [(UITextField *)nextTF becomeFirstResponder];
        } else {
            [textField resignFirstResponder];
        }
    }
    
    return YES;
}

#pragma mark - Event
- (void)viewDidClicked {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
- (void)keyboardWillShow:(NSNotification *)notif {
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    [UIView animateWithDuration:0.25 animations:^{
        UIEdgeInsets edgeInsets = self.scrollViewForHandleKeyboard.contentInset;
        edgeInsets.bottom = keyboardSize.height + 10;
        [self.scrollViewForHandleKeyboard setContentInset:edgeInsets];
    } completion:nil];
    
}
- (void)keyboardWillHide:(NSNotification *)notif {
    [UIView animateWithDuration:0.25 animations:^{
        UIEdgeInsets edgeInsets = self.scrollViewForHandleKeyboard.contentInset;
        edgeInsets.bottom = 0;
        [self.scrollViewForHandleKeyboard setContentInset:edgeInsets];
    } completion:nil];
}
@end
